

********************************************************************************
* 
** LEHD EVENT STUDY
* 
********************************************************************************

local controls="i.year"
local outcomelist="anyearn_allst earn"

use `outcomelist' evicted qdum* evictXq* pik black hispanic female ///
 noprior joint acs_placebo state_ind* relquarter year using "${build}/linked_lehd_event_study_panel.dta" /// 
 if evicted~=. & acs_placebo==0, clear
 
cap frame change default
cap frame drop results 
cap frame drop disclosure 
frame create results strL(label) evict nonevict q_plot
frame create disclosure strL(group) states obscount 

gen all=1

foreach pop in "all"  {
	foreach var of varlist  `outcomelist' {
		noi disp "`var' `pop`''"
		local l=41
		
		reg `var' evicted qdum2-qdum`l' evictXq2-evictXq`l' `controls' if `pop'==1

		*Calculate omitted group baseline mean
		sum `var' if evicted==0 & relquarter==-16  & e(sample)
		local basemean=r(mean)
		
		* adding period 1 (first) estimates
		local rel_q=1-17
		local q_plot=`rel_q' 
		local noevict_plot= `basemean'
		local evict_plot=`basemean'+ _b[evicted] 
		local label="Event Study Estimates, Pop: `pop', Outcome: `var'" 
		frame post results ("`label'")  (`evict_plot') (`noevict_plot') (`q_plot')
		
		foreach num of numlist 2(1)`l' {
			local rel_q=`num'-17
			local q_plot=`rel_q' 
			local noevict_plot= `basemean'+_b[qdum`num']
			local evict_plot=`basemean'+ _b[evicted]+_b[qdum`num']+_b[evictXq`num'] 
			local label="Event Study Estimates, Pop: `pop', Outcome: `var'" 
			frame post results ("`label'")  (`evict_plot') (`noevict_plot') (`q_plot')
		}
		
	}
}

frame results: save "${build}/event_study_estimates_LEHD.dta", replace 


********************************************************************************
* 
** HMIS EVENT STUDY
* 
********************************************************************************

local controls="i.year"
local outcomelist=" em_shelter shelter "

use `outcomelist' evicted qdum* evictXq* pik black hispanic female  ///
 noprior joint acs_placebo age ageatcase relyr year using "${build}/linked_hmis_event_study_panel.dta" /// 
 if evicted~=. & acs_placebo==0, clear
 
cap frame change default
cap frame drop results 
frame create results strL(label) evict nonevict q_plot

gen all=1

foreach pop in "all"  {
	foreach var of varlist  `outcomelist' {
		noi disp "`var' `pop`''"
		local l=5
		
		reg `var' evicted qdum2-qdum`l' evictXq2-evictXq`l' `controls' if `pop'==1

		*Calculate omitted group baseline mean
		sum `var' if evicted==0 & relyr==-1  & e(sample)
		local basemean=r(mean)
		
		* adding period 1 (first) estimates
		local rel_q=1-2
		local q_plot=`rel_q' 
		local noevict_plot= `basemean'
		local evict_plot=`basemean'+ _b[evicted] 
		local label="Event Study Estimates, Pop: `pop', Outcome: `var'" 
		frame post results ("`label'")  (`evict_plot') (`noevict_plot') (`q_plot')
		
		foreach num of numlist 2(1)`l' {
			local rel_q=`num'-2
			local q_plot=`rel_q' 
			local noevict_plot= `basemean'+_b[qdum`num']
			local evict_plot=`basemean'+ _b[evicted]+_b[qdum`num']+_b[evictXq`num'] 
			local label="Event Study Estimates, Pop: `pop', Outcome: `var'" 
			frame post results ("`label'")  (`evict_plot') (`noevict_plot') (`q_plot')
		}	
	}
}

frame results: save "${build}/event_study_estimates_HMIS.dta", replace 


********************************************************************************
* 
** MARF EVENT STUDY
* 
********************************************************************************

local controls="i.year"
local loadoutcomelist=" obs_this_year   not_at_eviction_address  any_diff_state_evic  avg_marf_all_hh_pov_rate "

use `loadoutcomelist' evicted ydum* evictXy* pik black hispanic female at_evic_n2_p1 ///
 noprior joint acs_placebo age ageatcase relyear year using "${build}/linked_marf_event_study_panel.dta" /// 
 if evicted~=. & acs_placebo==0, clear

gen not_at_evic_o_unobs = not_at_evic == 1 | obs == 0
 local outcomelist=" obs_this_year  not_at_evic_o_unobs  not_at_eviction_address  any_diff_state_evic  avg_marf_all_hh_pov_rate "

cap frame change default
cap frame drop results 
frame create results strL(label) evict nonevict q_plot

gen all=1
keep if relyear>=-4 & relyear <= 6
keep if at_evic_n2_p1 == 1
keep if age >= 18 & age <= 55

foreach pop in "all"   {
	foreach var of varlist  `outcomelist' {
		noi disp "`var' `pop`''"
		local l=11
		
		reg `var' evicted ydum2-ydum`l' evictXy2-evictXy`l' `controls' if `pop'==1

		*Calculate omitted group baseline mean
		sum `var' if evicted==0 & relyear==-4  & e(sample)
		local basemean=r(mean)
		
		* adding period 1 (first) estimates
		local rel_q=1-5
		local q_plot=`rel_q' 
		local noevict_plot= `basemean'
		local evict_plot=`basemean'+ _b[evicted] 
		local label="Event Study Estimates, Pop: `pop', Outcome: `var'" 
		frame post results ("`label'")  (`evict_plot') (`noevict_plot') (`q_plot')
		
		foreach num of numlist 2(1)`l' {
			local rel_q=`num'-5
			local q_plot=`rel_q' 
			local noevict_plot= `basemean'+_b[ydum`num']
			local evict_plot=`basemean'+ _b[evicted]+_b[ydum`num']+_b[evictXy`num'] 
			local label="Event Study Estimates, Pop: `pop', Outcome: `var'" 
			frame post results ("`label'")  (`evict_plot') (`noevict_plot') (`q_plot')
		}	
	}
}

frame results: save "${build}/event_study_estimates_MARF.dta", replace 


********************************************************************************
* 
** COMBINE EVENT STUDIES
* 
********************************************************************************

use "${build}/event_study_estimates_LEHD.dta", clear 
gen data = "LEHD"

append using "${build}/event_study_estimates_HMIS.dta"
replace data = "HMIS" if mi(data)

append using "${build}/event_study_estimates_MARF.dta"
replace data = "MARF" if mi(data)

export delimited "${Inputs}\Cook_lehd_hmis_marf_eventstudy.csv", replace



